using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace SharpGL.Controls
{
    /// <summary>
    /// Summary description for GLColourPicker.
    /// </summary>
    public class GLColourPicker : System.Windows.Forms.Control
    {
        #region Private Fields

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        float theWidth = 0;
        float theHeight = 0;

        #endregion Private Fields

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }

        #endregion Component Designer generated code

        #region Protected Methods

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }

            base.Dispose(disposing);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            float width = e.ClipRectangle.Width;
            float height = e.ClipRectangle.Height;

            Graphics graphics = e.Graphics;
            using (Bitmap bmp = new Bitmap((int)width, (int)height, PixelFormat.Format32bppRgb))
            {

                float red = 0, green = 0, blue = 0;

                float redadd = 255.0f / width;
                float greenadd = 255.0f / (height / 2);
                float blueadd = 255.0f / (height / 2);

                int y = 0;
                for (y = 0; y < (e.ClipRectangle.Height / 2); y++)
                {
                    for (int x = 0; x < e.ClipRectangle.Width; x++)
                    {
                        red += redadd;
                        bmp.SetPixel(x, y, Color.FromArgb((int)red, (int)green, (int)blue));
                    }
                    green += greenadd;
                    red = 0;
                }
                for (; y < e.ClipRectangle.Height; y++)
                {
                    for (int x = 0; x < e.ClipRectangle.Width; x++)
                    {
                        red += redadd;
                        bmp.SetPixel(x, y, Color.FromArgb((int)red, (int)green, (int)blue));
                    }
                    green -= greenadd;
                    blue += blueadd;
                    red = 0;
                }

                graphics.DrawImage(bmp, 0, 0);
            }

            // Calling the base class OnPaint
            base.OnPaint(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            //	We need to know the size of the control so we can
            //	have the correctly variables for a function that selects the
            //	colour.

            theWidth = Width;
            theHeight = Height;

            base.OnSizeChanged(e);
        }

        #endregion Protected Methods

        #region Constructors

        public GLColourPicker()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
        }

        #endregion Constructors
    }
}
